package com.flyco.modules.attendance.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.common.api.vo.Result;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.modules.attendance.entity.UmsCheckinout;
import com.flyco.modules.attendance.service.IUmsCheckinoutService;
import com.flyco.modules.attendance.vo.CheckinoutVO;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 员工打卡记录表
 * @Author: flyco
 * @Date:   2022-11-02
 * @Version: V1.0
 */
@Slf4j
@Api(tags="员工打卡记录表")
@RestController
@RequestMapping("/f/attendance/umsCheckinout")
public class UmsCheckinoutController extends BaseController<UmsCheckinout, IUmsCheckinoutService> {
	@Autowired
	private IUmsCheckinoutService umsCheckinoutService;
	@Autowired
	private IStmStoreInfoService stmStoreInfoService;

	/**
	 * 分页列表查询
	 *
	 * @param umsCheckinout
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "员工打卡记录表-分页列表查询")
	@ApiOperation(value="员工打卡记录表-分页列表查询", notes="员工打卡记录表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(UmsCheckinout umsCheckinout,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<UmsCheckinout> queryWrapper = QueryGenerator.initQueryWrapper(umsCheckinout, req.getParameterMap());
		queryWrapper.orderByDesc("checkinout_time");
		Page<UmsCheckinout> page = new Page<UmsCheckinout>(pageNo, pageSize);
		IPage<UmsCheckinout> pageList = umsCheckinoutService.page(page, queryWrapper);
		if(!pageList.getRecords().isEmpty()){
			List<UmsCheckinout> records = pageList.getRecords();
			//查询店铺
			List<Long> storeIds = records.stream().map(UmsCheckinout::getStoreId).distinct().collect(Collectors.toList());
			Map<Long, String> storeMap = stmStoreInfoService.list(Wrappers.lambdaQuery(StmStoreInfo.class).in(StmStoreInfo::getStoreId, storeIds)).stream().collect(Collectors.toMap(StmStoreInfo::getStoreId, StmStoreInfo::getName));

			//组装返回值
			List<CheckinoutVO> collect = records.stream().map(one -> {
				CheckinoutVO checkinoutVO = new CheckinoutVO();
				BeanUtil.copyProperties(one,checkinoutVO);
				String storeName = storeMap.get(one.getStoreId());
				if(StrUtil.isNotBlank(storeName)){
					checkinoutVO.setStoreName(storeName);
				}
				//图片路径修改
				//checkinoutVO.setPicture(getUrl(one.getPicture()));
				return checkinoutVO;
			}).collect(Collectors.toList());
			IPage<CheckinoutVO> result = new Page<>(pageList.getCurrent(),pageList.getSize(),pageList.getTotal());
			result.setRecords(collect);
			return Result.ok(result);

		}
		return Result.ok(pageList);
	}

	/*private String getUrl(String url){
		//https://flycopic.oss-cn-hangzhou.aliyuncs.com/store-manage-dev/app/20221031/ehr/6_15-23-06.jpg
		if(url.contains("flycopic.oss-cn-hangzhou.aliyuncs.com")){
			return url.replace("flycopic.oss-cn-hangzhou.aliyuncs.com","pic.flyco.net.cn");
		}
		return url;
	}*/
	
	/**
	 * 添加
	 *
	 * @param umsCheckinout
	 * @return
	 */
	@AutoLog(value = "员工打卡记录表-添加")
	@ApiOperation(value="员工打卡记录表-添加", notes="员工打卡记录表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody UmsCheckinout umsCheckinout) {
		umsCheckinoutService.save(umsCheckinout);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param umsCheckinout
	 * @return
	 */
	@AutoLog(value = "员工打卡记录表-编辑")
	@ApiOperation(value="员工打卡记录表-编辑", notes="员工打卡记录表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody UmsCheckinout umsCheckinout) {
		umsCheckinoutService.updateById(umsCheckinout);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "员工打卡记录表-通过id删除")
	@ApiOperation(value="员工打卡记录表-通过id删除", notes="员工打卡记录表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		umsCheckinoutService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "员工打卡记录表-批量删除")
	@ApiOperation(value="员工打卡记录表-批量删除", notes="员工打卡记录表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.umsCheckinoutService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "员工打卡记录表-通过id查询")
	@ApiOperation(value="员工打卡记录表-通过id查询", notes="员工打卡记录表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		UmsCheckinout umsCheckinout = umsCheckinoutService.getById(id);
		return Result.ok(umsCheckinout);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param umsCheckinout
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, UmsCheckinout umsCheckinout) {
      return super.exportXls(request, umsCheckinout, UmsCheckinout.class, "员工打卡记录表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, UmsCheckinout.class);
  }

}
